<script setup>
import { ref, nextTick, onMounted } from "vue";
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入折线图图表，图表后缀都为 Chart
import { BarChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 标签自动布局、全局过渡动画等特性
import { UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";
import dataGeo from "@/assets/address/data_geo.json";

// 注册必须的组件
echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  UniversalTransition,
  CanvasRenderer,
]);

// 接收数据
const props = defineProps({
  ranking: {
    type: Array,
    default: function () {
      return [];
    },
  },
});

let entries = [];
let num = [];
let marks = [];
let total = [];
const initData = () => {
  for (let i = 0; i < props.ranking.length; i++) {
    let name;
    dataGeo.some((item) => {
      if (String(item.value) === props.ranking[i].id) {
        name = item.label;
        return true;
      }
    });
    entries.push(name);
    num.push(i + 1);
    marks.push(props.ranking[i].mark);
    total.push(100);
  }
};
initData();

const optionForm = {
  grid: {
    top: "1%",
    left: "18%",
    bottom: "1%",
  },
  xAxis: {
    show: false,
  },
  yAxis: [
    {
      type: "category",
      inverse: true,
      data: [],
      // 不显示y轴的线
      axisLine: {
        show: false,
      },
      // 不显示刻度
      axisTick: {
        show: false,
      },
      splitLine: {
        show: false,
      },
      // 把刻度标签里面的文字颜色设置为白色
      axisLabel: {
        show: true,
        textStyle: {
          color: "#fff",
        },
      },
    },
    {
      type: "category",
      inverse: true,
      axisTick: "none",
      axisLine: "none",
      show: true,
      axisLabel: {
        textStyle: {
          color: "#ffffff",
          fontSize: "12",
        },
      },
      data: [],
    },
  ],
  series: [
    {
      name: "条",
      type: "bar",
      zlevel: 1,
      data: [],
      yAxisIndex: 0,
      // 柱子之间的距离
      barCategoryGap: 2,
      //柱子的宽度
      barWidth: 7,
      // 显示柱子内的文字
      label: {
        show: true,
        position: "inside",
        // {c} 会自动的解析为 数据  data里面的数据
        formatter: "{c}%",
      },
      itemStyle: {
        normal: {
          barBorderRadius: 30,
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            {
              offset: 0,
              color: "rgb(57,89,255,1)",
            },
            {
              offset: 1,
              color: "rgb(46,200,207,1)",
            },
          ]),
        },
      },
    },
    {
      name: "框",
      type: "bar",
      barCategoryGap: 2,
      barWidth: 11,
      yAxisIndex: 1,
      data: [],
      itemStyle: {
        normal: {
          color: "rgba(24,31,68,1)",
          barBorderRadius: 30,
        },
      },
    },
  ],
};

const option = ref({ ...optionForm });

let containerRef = ref(null);
const initChart = () => {
  const myChart = echarts.init(containerRef.value);
  option.value.yAxis[0].data = entries;
  option.value.yAxis[1].data = num;
  option.value.series[0].data = marks;
  option.value.series[1].data = total;
  myChart.setOption(option.value);
};

onMounted(() => {
  nextTick(() => {
    initChart();
  });
});
</script>

<template>
  <div id="container" ref="containerRef"></div>
</template>

<style lang="scss" scoped>
#container {
  height: 100%;
  width: 100%;
}
</style>